package leetcode;

import java.util.Stack;

public class Leetcode1047 {
    public String removeDuplicates(String S) {
        if(S.length()==0&&S.length()==1){
            return S;
        }
        Stack<Character> s = new Stack<>();
        for (int i = 0; i < S.length(); i++) {
            char tmp = S.charAt(i);
            if(!s.isEmpty()&&s.peek()==tmp){
                s.pop();
            }else{
                s.push(tmp);
            }
        }
        StringBuilder sb = new StringBuilder();
        while(!s.isEmpty()){
            sb.append(s.pop());
        }
        return sb.reverse().toString();
    }
}
